import { Injectable } from '@nestjs/common';
import { Dynamic_Create_Dto, Dynamic_Update_Dto } from './assets/dynamic.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Dynamic } from './assets/dynamic.entity';
import { Repository } from 'typeorm';
import { Dynamic_Find_Dto } from './assets/dynamic.dto';
import { util_Db_Add, util_Db_Delete, util_Db_PageList, util_Db_Update } from '@/common/util/db';

@Injectable()
export class Dynamic_Service {
  constructor(
    @InjectRepository(Dynamic)
    private dynamic_Repository: Repository<Dynamic>,
  ) {}

  async dynamic_Create(body: Dynamic_Create_Dto) {
    return await util_Db_Add({
      createDto: body,
      repository: this.dynamic_Repository,
    });
  }

  async dynamic_FindByPage(query: Dynamic_Find_Dto) {
    return await util_Db_PageList<Dynamic>({
      queryBuilder: this.dynamic_Repository.createQueryBuilder('dynamic'),
      query,
    });
  }

  async dynamic_FindOneById(id: number) {
    return await this.dynamic_Repository
      .createQueryBuilder('dynamic')
      .andWhere(`dynamic.id = :id`, { id: id })
      .getOne();
  }

  async dynamic_Update(body: Dynamic_Update_Dto) {
    return await util_Db_Update({
      findOne: () => this.dynamic_FindOneById(body.id),
      updateDto: body,
      repository: this.dynamic_Repository,
    });
  }

  async dynamic_Remove(id: number) {
    return await util_Db_Delete({
      findOne: () => this.dynamic_FindOneById(id),
      id,
      repository: this.dynamic_Repository,
    });
  }
}
